$(function () {
    // 默认加载部门树
    vm.getDept();
    // vm.init();
    // vm.getTreeList();
    // vm.getStore();
    // setInterval(vm.getInfo(), 1000);
});
var ztree;
//部门树
var deptSetting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: '01'
        },
        key: {
            url: "nourl",
            name: "complexName" //name 与 typeName 绑定
        },
    },
    callback: {
        onClick: outTreeCheck
    }
};

function outTreeCheck(e, treeObj, treeNode) {
    // 只有用户点击产线菜单时显示
    vm.showButton= (treeNode.deptLevel==3);
    //设置当前组织ID
    vm.q.curDeptId = treeNode.deptId;
    vm.q.deptCode = treeNode.dcode;
    // 执行查询
    vm.search();
}

var myChart;
var vm = new Vue({
    el:"#rrapp",
    data:{
        treeList: [],
        showList:false,
        showButton:false, // 是否展示查看二维平面图按钮
        q: {
            curDeptId: '',
            deptCode: '' // 部门编码
        },
        deptId:'01', // 用户点击左侧菜单树时当前组织ID
        echartDom:"",
        //报修工单统计
        workOrderStat:{
            un: 0,
            ing:0,
            ed:0
        },
        // 维护计划统计
        maintainPlanStat:{
            un: 0,
            ing:0,
            ed:0
        },
        option:{
            color: ['#f10606','#6ec56f','#f8e567','#a5a5a5','#4472c4','#6ec56f','#6ec56f'],
            title: {
                text: '设备状态数量分布统计',
                left: 'center'
            },
            tooltip: {
                trigger: 'item',
                formatter: '{b}: {c} ({d}%)'
            },
            legend: {
                data:['运行', '空闲','停机','未知'],
                orient: 'vertical',
                right: 20
            },
            labelLine: {
                length: 50,
            },
            label: {
                formatter: '{a|{a}}{abg|}\n{hr|}\n  {b|{b}：}{c}  {per|{d}%}  ',
                backgroundColor: '#F6F8FC',
                borderColor: '#8C8D8E',
                borderWidth: 1,
                borderRadius: 4,

                rich: {
                    a: {
                        color: '#6E7079',
                        lineHeight: 22,
                        align: 'center'
                    },
                    hr: {
                        borderColor: '#8C8D8E',
                        width: '100%',
                        borderWidth: 1,
                        height: 0
                    },
                    b: {
                        color: '#4C5058',
                        fontSize: 14,
                        fontWeight: 'bold',
                        lineHeight: 33
                    },
                    per: {
                        color: '#fff',
                        backgroundColor: '#4C5058',
                        padding: [3, 4],
                        borderRadius: 4
                    }
                }
            },
            series: [
                {
                    name: '设备状态数分布',
                    type: 'pie',
                    radius: '65%',
                    center: ['50%', '50%'],
                    selectedMode: 'single',
                    data: [
                        {value: 335, name: '运行'},
                        {value: 310, name: '空闲'},
                        {value: 234, name: '停机'},
                        {value: 135, name: '未知'}
                    ]
                }
            ]
        }
    },
    methods:{
        getDept: function () {
            //加载部门树
            Ajax.request({
                url: '../sys/user/treeDeptBySCX',
                async: false,
                successCallback: function (r) {
                    ztree = $.fn.zTree.init($("#storeTree"), deptSetting, r.list);
                    var node = ztree.getNodeByParam("deptId", '01');
                    if (node != null) {
                        vm.q.curDeptId = node.deptId;
                        if(node.dcode){
                            vm.q.deptCode = node.dcode;
                        }
                        ztree.selectNode(node);
                        ztree.expandNode(node);
                    }else{
                        ztree.expandAll(true);
                    }
                    // 执行查询
                    vm.search();
                }
            });
        },
        search: function () {
            var param = {};
            if(vm.q.curDeptId){
                param.curDeptId = vm.q.curDeptId;
            }
            myChart.showLoading();
            Ajax.request({
                url: "../device/stat/status",
                params: JSON.stringify(param),
                type: "POST",
                contentType: "application/json",
                successCallback: function (res) {
                    myChart.hideLoading();
                    var option = {
                        title: {
                            subtext: ''
                        },
                        series: {
                            data: res.devStatusStat.seriesData
                        },
                        legend: {
                            data: res.devStatusStat.legendData
                        }
                    };

                    if(res.devStatusStat.seriesData.length == 0){
                        option.title.subtext = '当前组织下暂无设备信息';
                    }
                    myChart.setOption(option);
                }
            });
            // 加载当前组织下的报修工单和维护计划执行情况
            if(vm.q.deptCode){
                Ajax.request({
                    url: "../device/stat/maintainNum?lineCode=" + vm.q.deptCode,
                    type: "GET",
                    contentType: "application/json",
                    successCallback: function (res) {
                        if(res.code == 0){
                            vm.workOrderStat = res.deptStat.workOrderStat;
                            vm.maintainPlanStat = res.deptStat.maintainPlanStat;
                        }else{
                            iview.Message.error(r.msg);
                        }
                    }
                });
            }
        },
        //转到图片展示页面，可以带参数
        turnToPic:function(){
            Ajax.request({
                url: '../devPlane/check?lineCode=' + vm.q.curDeptId,
                type:'GET',
                async: false,
                contentType: "application/json",
                successCallback: function (r) {
                    if(r.code == 0){
                        window.open('lineMonitor.html?lineCode=' + vm.q.curDeptId)
                    }else{
                        iview.Message.error(r.msg);
                    }
                }
            });
        },
        getTreeList: function () {
            Ajax.request({
                url: '../../../data/monitortree.json',
                async: false,
                successCallback: function (r) {
                    vm.treeList = r.list;
                }
            });
        },
        //加载列表页树
        getStore: function () {
            deptZtree = $.fn.zTree.init($("#storeTree"), deptSetting, vm.treeList);
            deptZtree.expandAll(true);
        }
    },
    mounted: function(){
        // 初始化部门树
        myChart = echarts.init(document.getElementById("container"));
        myChart.setOption(this.option,true)  //setOption 用this.option中的数据开始作图
    }
});
